package com.bridge.applets.config;

import com.bridge.applets.utils.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
@Configuration
public class FileUploadConfig implements WebMvcConfigurer {

    @Autowired
    public FileUploadUtil fileUploadUtil;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将匹配上/files/**虚拟路径的url映射到文件上传到服务器的目录，获取静态资源
        String uploadPattern = fileUploadUtil.uploadPattern;
        String filePath = fileUploadUtil.filePath;
        log.info("uploadPattern:{},filePath:{}", uploadPattern, Paths.get(filePath).toAbsolutePath());

        registry.addResourceHandler("/" + uploadPattern + "/**")
                .addResourceLocations("file:" + filePath)
                .setCachePeriod(0);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}
